package xx.rabbitmq.conf;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConfirmConfig {

    /**
     * 交换机名称
     */
    public static final String CONFIRM_EXCHANGE = "CONFIRM_EXCHANGE";
    /**
     * 队列名称
     */
    public static final String CONFIRM_QUEUE = "CONFIRM_QUEUE";
    /**
     * 队列交换机绑定KEY
     */
    public static final String CONFIRM_QUEUE_EXCHANGE_KEY = "CONFIRM_QUEUE_EXCHANGE_KEY";

    /**
     * 设置队列名称及相关属性
     * 一般设置一下队列的持久化就好,其余两个就是默认false
     * @return
     */
    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return new Queue(CONFIRM_QUEUE, true);
    }

    /**
     * Direct交换机
     * @return
     */
    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
        return new DirectExchange(CONFIRM_EXCHANGE, true, false);
    }

    /**
     * 绑定  将队列和交换机绑定, 并设置用于匹配键：CONFIRM_QUEUE_EXCHANGE_KEY
     * @return
     */
    @Bean("bindingConFirm")
    public Binding bindingConFirm(@Qualifier("confirmQueue") Queue confirmQueue,
                                 @Qualifier("confirmExchange") DirectExchange confirmExchange) {
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_QUEUE_EXCHANGE_KEY);
    }

}
